package com.tools.web.interceptor.limiting;

import com.tools.common.object.Note;

import java.util.Objects;

/**
 * 限流拦截器所需的配置项参数包装类
 * */
@Note("限流拦截器所需的配置项参数包装类")
public class RequestLimitSource {

    @Note("请求次数存储在存储里的 key 的前缀")
    private String cachePrefix;

    @Note("请求次数达到上限后给客户端响应的信息内容。最好是 JSON 字符串")
    private String limitResponseMessage;

    @Note("是否开启 【请求次数达到最大上限就封禁账户的功能】，注意这只是开关之一，不是决定性的启动开关")
    private boolean useAuthHandler;

    @Note("封禁账号后，这里决定响应给客户端什么内容，可以为空，为空则不返回内容")
    private String disabledResponseMessage;

    public RequestLimitSource() {}

    public static RequestLimitSource of() {
        return new RequestLimitSource();
    }

    public String getCachePrefix() {
        return cachePrefix;
    }

    public void setCachePrefix(String cachePrefix) {
        this.cachePrefix = cachePrefix;
    }

    public String getLimitResponseMessage() {
        return limitResponseMessage;
    }

    public void setLimitResponseMessage(String limitResponseMessage) {
        this.limitResponseMessage = limitResponseMessage;
    }

    public boolean isUseAuthHandler() {
        return useAuthHandler;
    }

    public void setUseAuthHandler(boolean useAuthHandler) {
        this.useAuthHandler = useAuthHandler;
    }

    public String getDisabledResponseMessage() {
        return disabledResponseMessage;
    }

    public void setDisabledResponseMessage(String disabledResponseMessage) {
        this.disabledResponseMessage = disabledResponseMessage;
    }


    @Override
    public String toString() {
        return "RequestLimitSource{" +
                "cachePrefix='" + cachePrefix + '\'' +
                ", limitResponseMessage='" + limitResponseMessage + '\'' +
                ", useAuthHandler=" + useAuthHandler +
                ", disabledResponseMessage='" + disabledResponseMessage + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RequestLimitSource that = (RequestLimitSource) o;
        return Objects.equals(cachePrefix, that.cachePrefix);
    }

    @Override
    public int hashCode() {
        return Objects.hash(cachePrefix);
    }

    public RequestLimitSource cachePrefix(String cachePrefix) {
        this.setCachePrefix(cachePrefix);
        return this;
    }

    public RequestLimitSource limitResponseMessage(String limitResponseMessage) {
        this.setLimitResponseMessage(limitResponseMessage);
        return this;
    }

    public RequestLimitSource useAuthHandler(boolean useAuthHandler) {
        this.setUseAuthHandler(useAuthHandler);
        return this;
    }

    public RequestLimitSource disabledResponseMessage(String disabledResponseMessage) {
        this.setDisabledResponseMessage(disabledResponseMessage);
        return this;
    }
}
